<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace kitego\extend\login\admin\impl;

use app\dao\setting\SystemAdminDao;
use kitego\extend\login\admin\AdminLoginBase;
use kitego\utils\captcha\CaptchaUtil;
use kitego\utils\clickcaptcha\ClickCaptchaUtil;

class AdminPasswordLoginImpl extends AdminLoginBase
{
    public function login($params)
    {
        // 校验验证码
        if ($params['captchaType'] == 1 && !app()->make(CaptchaUtil::class)->check($params['captcha'])) {
            exception('验证码错误');
        }

        // 校验点选验证码
        if ($params['captchaType'] == 2 && !app()->make(ClickCaptchaUtil::class)->check($params['captchaId'], $params['captcha'])) {
            exception('点选验证码错误');
        }

        // 查管理员
        $systemAdmin = (new SystemAdminDao())->getSystemAdminByAccount($params['account']);
        if (!$systemAdmin || !password_verify($params['password'], $systemAdmin['password'])) {
            exception('账号密码错误');
        }

        // 校验账号有效性
        if ($systemAdmin['status'] == 2 || $systemAdmin['delete_flag'] == 2) {
            exception('账户已被禁用|已删除');
        }

        $systemAdmin['avatar'] = request()->domain() . $systemAdmin['avatar'];

        return $this->generateJwtToken($systemAdmin);
    }
}